package com.hxzy.controller.admin;
import cn.hutool.core.util.StrUtil;
import com.hxzy.common.ExistsValue;
import com.hxzy.common.annon.StopWatch;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import com.hxzy.controller.BaseController;
import com.hxzy.dto.SysMenuDTO;
import com.hxzy.dto.SysRoleSaveDTO;
import com.hxzy.entity.SysMenu;
import com.hxzy.service.ExistsValueService;
import com.hxzy.service.SysMenuService;
import com.hxzy.vo.MenuTreeVO;
import com.hxzy.vo.SysMenuTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

@Api(tags = "菜单API")
@RestController
@RequestMapping(value = "/api/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ExistsValueService existsValueService;
    /**
     * 读取所有的菜单(给角色分配权限用)
     * @return
     */
    @ApiOperation(value ="读取所有的菜单(给角色分配权限用)" )
    @GetMapping(value = "/tree")
    public R  loadTreeMenu(){
        List<MenuTreeVO> menuTreeVOList = this.sysMenuService.loadMenuTree();
        return R.okHasData(menuTreeVOList);
    }

    @ApiOperation(value = "菜单查询")
    @GetMapping(value = "/data")
    public R search(SysMenuDTO dto){
        List<SysMenuTreeVO> sysMenuTreeVOList=this.sysMenuService.search(dto);
        return R.okHasData(sysMenuTreeVOList);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{menuId}")
    public R findById(@PathVariable(value = "menuId") BigDecimal menuId){

        if(menuId.intValue()<=0){
            return R.build(AckCode.SYSTEM_PARAM_FAIL);
        }

       SysMenuTreeVO  sysMenuTreeVO=this.sysMenuService.findById(menuId);
       if(sysMenuTreeVO==null){
           return R.build(AckCode.NOT_FOUND_DATA);
       }
       return R.okHasData(sysMenuTreeVO);
    }


    @ApiOperation(value = "新增菜单")
    @PostMapping
    public  R add(@RequestBody @Valid SysMenu sysMenu){
        if(StrUtil.isNotBlank(sysMenu.getPerms())){
            //验证权限标识唯一性
            ExistsValue numberKey=new ExistsValue();
            numberKey.setTableName("SYS_MENU");
            numberKey.setColumnName("perms");
            numberKey.setColumnValue(sysMenu.getPerms());
            boolean numberCount=this.existsValueService.searchCount(numberKey);
            if(numberCount){
                R<Object> build = R.build(AckCode.FAIL);
                build.setMsg("权限标识已被占用");
                return build;
            }
        }

        boolean result=this.sysMenuService.save(sysMenu);
        return super.toAjax(result);
    }

    @StopWatch
    @ApiOperation(value = "修改菜单")
    @PutMapping
    public  R update(@RequestBody @Valid SysMenu sysMenu){

        //验证权限标识唯一性
        ExistsValue numberKey=new ExistsValue();
        numberKey.setTableName("SYS_MENU");
        numberKey.setPrimaryKeyName("MENU_ID");
        numberKey.setPrimaryKeyValue(sysMenu.getMenuId());

        numberKey.setColumnName("perms");
        numberKey.setColumnValue(sysMenu.getPerms());
        boolean numberCount=this.existsValueService.searchCount(numberKey);
        if(numberCount){
            R<Object> build = R.build(AckCode.FAIL);
            build.setMsg("权限标识已被占用");
            return build;
        }
        boolean result=this.sysMenuService.updateById(sysMenu);
        return super.toAjax(result);
    }


}
